package com.mars.foodserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mars.foodserver.constant.NumberConstant;
import com.mars.foodserver.entity.Food;
import com.mars.foodserver.entity.FoodCollect;
import com.mars.foodserver.entity.FoodLike;
import com.mars.foodserver.entity.FoodType;
import com.mars.foodserver.mapper.FoodCollectMapper;
import com.mars.foodserver.response.FoodLikeResponse;
import com.mars.foodserver.service.IFoodCollectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mars.foodserver.service.IFoodService;
import com.mars.foodserver.service.IFoodTypeService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wq
 * @since 2021-07-21
 */
@Service
public class FoodCollectServiceImpl extends ServiceImpl<FoodCollectMapper, FoodCollect> implements IFoodCollectService {

    @Resource
    private IFoodService foodService;

    @Resource
    private IFoodTypeService foodTypeService;

    @Override
    public void collect(Integer userId, Integer foodId, Integer type) {
        if (type.equals(NumberConstant.ONE)) {
            //收藏
            FoodCollect foodCollect = baseMapper.selectOne(new LambdaQueryWrapper<FoodCollect>().
                    eq(FoodCollect::getUserId, userId).eq(FoodCollect::getFoodId, foodId));
            if (Objects.isNull(foodCollect)) {
                FoodCollect like = new FoodCollect();
                like.setFoodId(foodId);
                like.setUserId(userId);
                save(like);
            }
        } else {
            baseMapper.delete(new LambdaQueryWrapper<FoodCollect>().
                    eq(FoodCollect::getFoodId, foodId).eq(FoodCollect::getUserId, userId));
        }
    }

    @Override
    public boolean isCollectFood(Integer userId, Integer id) {

        FoodCollect one = baseMapper.selectOne(new LambdaQueryWrapper<FoodCollect>().
                eq(FoodCollect::getUserId, userId).eq(FoodCollect::getFoodId, id));
        return Objects.nonNull(one);
    }

    @Override
    public List<FoodLikeResponse> collectList(Integer userId) {
        List<FoodCollect> foodLikes = baseMapper.selectList(new LambdaQueryWrapper<FoodCollect>().eq(FoodCollect::getUserId, userId));
        if (!CollectionUtils.isEmpty(foodLikes)) {
            return foodLikes.stream().map(x -> {
                FoodLikeResponse response = new FoodLikeResponse();
                response.setFoodId(x.getFoodId());
                Food food = foodService.detail(x.getFoodId());
                if (Objects.nonNull(food)) {
                    response.setImgUrl(food.getImgUrl());
                    response.setName(food.getName());
                    FoodType detail = foodTypeService.detail(food.getFoodTypeId());
                    if (Objects.nonNull(detail)) {
                        response.setFoodTypeName(detail.getName());
                    }
                }
                return response;
            }).collect(Collectors.toList());
        }
        return null;
    }
}
